﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using Cosmetic;

namespace WebApp.Controls
{
    public partial class newcalsss : System.Web.UI.UserControl
    {
        private NewClassManger m_CategoryManager = new NewClassManger();
        public delegate void SelectedCategoryChangedHandler(newclass aCategory);
        public event SelectedCategoryChangedHandler SelectedLv1CategoryChanged;
        public event SelectedCategoryChangedHandler SelectedLv2CategoryChanged;
        private newclass t_Category;

        public NewClassManger.ETopCategory TopCategoryName { get; set; }

        public EBool ShowLv2Category { get; set; }
        public EBool ShowLv3Category { get; set; }

        private newclass m_TopCategory;
        public newclass TopCategory
        {
            get { return m_TopCategory; }
        }

        public newclass SaveCategory
        {
            get { return t_Category; }
            set { t_Category = value; }
        }

        public newclass SelectedCategory
        {
            get
            {
                
                if (this.DropDownList2.Visible && this.DropDownList2.Enabled && this.DropDownList2.SelectedValue != "*" && !String.IsNullOrEmpty(this.DropDownList2.SelectedValue))
                {
                    return this.m_CategoryManager.GetCategoryById(new Guid(this.DropDownList2.SelectedValue));
                }
                else if (this.DropDownList1.Visible && this.DropDownList1.SelectedValue != "*" && !String.IsNullOrEmpty(this.DropDownList1.SelectedValue))
                {
                    return this.m_CategoryManager.GetCategoryById(new Guid(this.DropDownList1.SelectedValue));
                }

                {
                    return null;
                }
            }
            set
            {
                this.Refresh();

              
                if (null == value || Guid.Empty == value.CategoryId)
                {
                    return;
                }

                if (this.DropDownList1.Items.Contains(new ListItem(value.Name, value.CategoryId.ToString())))
                {
                    this.DropDownList1.SelectedIndex = this.DropDownList1.Items.IndexOf(this.DropDownList1.Items.FindByValue(value.CategoryId.ToString()));
                }
                else
                {
                    if (this.DropDownList1 .Items.Contains(new ListItem(value.Name , value.SupCategoryId.ToString())))
                    {
                        this.DropDownList1.SelectedIndex = this.DropDownList1.Items.IndexOf(this.DropDownList1.Items.FindByValue(value.SupCategoryId.ToString()));
                        this.DropDownList1_SelectedIndexChanged1(null, null);
                        this.DropDownList2.SelectedIndex = this.DropDownList2.Items.IndexOf(this.DropDownList2.Items.FindByValue(value.CategoryId.ToString()));
                        this.DropDownList2_SelectedIndexChanged1(null, null);
                    }
                    else
                    {
                        this.AddNoDataItemToDropDownList(this.DropDownList1);
                        this.AddNoDataItemToDropDownList(this.DropDownList2);
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                this.Refresh();
            }
        }

        public void Refresh()
        {
          
            if (this.ShowLv2Category == EBool.True)
            {
                this.DropDownList1.AutoPostBack = true;
                this.DropDownList1.Enabled = true;
            }
            else
            {
                this.DropDownList1.AutoPostBack = false;
                this.DropDownList1.Enabled = true;
                this.DropDownList2.Visible = false;
             
            }

            if (null != this.SelectedLv1CategoryChanged)
            {
                this.DropDownList1.AutoPostBack = true;
            }
            if (null != this.SelectedLv2CategoryChanged)
            {
                this.DropDownList2.AutoPostBack = true;
            }
         


            this.m_TopCategory = m_CategoryManager.GetTopCategryByName(TopCategoryName);
            if (null != m_TopCategory)
            {
                this.BindDropDownList(this.DropDownList1, m_CategoryManager.GetSubCateogoriesBySupCategory(TopCategory));
            }
            else
            {
                this.m_CategoryManager.CheckTopCategory();
            }
        }

        private void BindDropDownList(DropDownList aDropDownList, List<newclass> aCategoryList)
        {
            aDropDownList.Items.Clear();

            if (aCategoryList.Count > 0)
            {
                aDropDownList.DataSource = aCategoryList;
                aDropDownList.DataTextField = "Name";
                aDropDownList.DataValueField = "CategoryId";
                aDropDownList.DataBind();
                aDropDownList.Enabled = true;
                aDropDownList.SelectedIndex = 0;
            }
            else
            {
                this.AddNoDataItemToDropDownList(aDropDownList);
            }

            if ("DropDownList1" == aDropDownList.ID)
            {
                this.DropDownList1_SelectedIndexChanged1(null, null);
            }
            if ("DropDownList2" == aDropDownList.ID)
            {
                this.DropDownList2_SelectedIndexChanged1(null, null);
            }
           
        }

        private void AddNoDataItemToDropDownList(DropDownList aDropDownList)
        {
            aDropDownList.Items.Add(new ListItem("No Data", string.Empty));
            aDropDownList.SelectedIndex = 0;
            aDropDownList.Enabled = false;
        }

     
        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            newclass theSelectedCategory;

            if (this.DropDownList1.SelectedValue == "*" || String.IsNullOrEmpty(this.DropDownList1.SelectedValue))
            {
                theSelectedCategory = null;
            }
            else
            {
                theSelectedCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.DropDownList1.SelectedValue));
                if (ShowLv2Category == EBool.True)
                {
                    if (String.IsNullOrEmpty(this.DropDownList1.SelectedValue))
                    {
                        this.DropDownList2.Items.Add(new ListItem("No Data", string.Empty));
                        this.DropDownList2.Enabled = false;
                        return;
                    }

                    this.BindDropDownList(this.DropDownList2, this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSelectedCategory));
                }
            }

            if (null != this.SelectedLv1CategoryChanged)
            {
                this.SelectedLv1CategoryChanged(theSelectedCategory);
            }

        }

        protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            newclass theSelectedCategory;

            if (this.DropDownList2.SelectedValue == "*" || String.IsNullOrEmpty(this.DropDownList2.SelectedValue))
            {
                theSelectedCategory = null;
            }
            else
            {
                theSelectedCategory = this.m_CategoryManager.GetCategoryById(new Guid(this.DropDownList2.SelectedValue));
            }

            if (null != this.SelectedLv2CategoryChanged)
            {
                this.SelectedLv2CategoryChanged(theSelectedCategory);
            }

        }

      
    }
}